package com.lys.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lys.commonutils.R;
import com.lys.eduservice.entity.EduCourse;
import com.lys.eduservice.entity.EduTeacher;
import com.lys.eduservice.entity.vo.CourseInfoVo;
import com.lys.eduservice.entity.vo.CoursePublishVo;
import com.lys.eduservice.entity.vo.CourseQuery;
import com.lys.eduservice.service.EduCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author lys
 * @since 2022-06-29
 */
@RestController
@Slf4j
@Api(tags = "课程信息管理")
@RequestMapping("/eduservice/educourse")
public class EduCourseController {

    @Autowired
    private EduCourseService eduCourseService;

    //添加课程信息
    @PostMapping("/addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        String courseId = eduCourseService.saveCourseInfo(courseInfoVo);
        return R.ok().data("courseId",courseId);
    }

    //根据id查询课程信息
    @ApiOperation(value = "查询课程信息接口")
    @GetMapping("/selectCourseById/{courseId}")
    public R selectCourseById(@PathVariable String courseId){
        CourseInfoVo courseInfoVo = eduCourseService.queryCourseById(courseId);
        return R.ok().data("courseInfoVo",courseInfoVo);
    }

    //修改课程信息
    @ApiOperation(value = "修改课程信息接口")
    @PostMapping("updateCourseById")
    public R updateCourseById(@RequestBody CourseInfoVo courseInfoVo){
          eduCourseService.updateCourseById(courseInfoVo);
             return R.ok();
    }

    //根据课程id查询课程确认信息
    @GetMapping("getPublishCourseInfo/{id}")
    public R getPublishCourseInfo(@PathVariable String id) {
        CoursePublishVo coursePublishVo = eduCourseService.publishCourseInfo(id);
        return R.ok().data("publishCourse",coursePublishVo);
    }

    //课程最终发布（修改课程状态）
    @GetMapping("/publishCourse/{id}")
    public R publishCourse(@PathVariable String id){
        EduCourse eduCourse =new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        boolean ret = eduCourseService.updateById(eduCourse);
        if (ret){
            return R.ok();
        }else {
            return R.error();
        }
    }

    /**
     * 分页查询课程列表
     * @param page
     * @param pageSize
     * @param courseQuery
     * @return
     */
    @PostMapping("pageCourseByCondition/{page}/{pageSize}")
    @ApiOperation(value = "根据条件分页查询讲师")
    public R pageCourseByCondition(@PathVariable long page, @PathVariable long pageSize,@RequestBody(required = false) CourseQuery courseQuery){
        //获取条件参数
        log.info(courseQuery.getName());
        String name = courseQuery.getName();
        String begin =courseQuery.getBegin();
        String end = courseQuery.getEnd();
        String status = courseQuery.getStatus();
        //分页构造器
        Page<EduCourse> pageInfo = new Page<>(page,pageSize);
        //条件构造器
        LambdaQueryWrapper<EduCourse> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name!=null,EduCourse::getTitle,name);
        queryWrapper.eq(status!=null, EduCourse::getStatus,status);
        queryWrapper.ge(begin!=null,EduCourse::getGmtCreate,begin);
        queryWrapper.le(end!=null,EduCourse::getGmtModified,end);
        queryWrapper.orderByAsc(EduCourse::getGmtCreate);

        eduCourseService.page(pageInfo,queryWrapper);
        Long total = pageInfo.getTotal();
        List<EduCourse> eduCourseList = pageInfo.getRecords();

        return  R.ok().data("total",total).data("eduCourseList",eduCourseList);
    }
    //根据id删除课程信息
    @DeleteMapping("/deleteCourseById/{id}")
    public R deleteCourseById(@PathVariable String id){
        int ret = eduCourseService.deleteCourseById(id);
        if (ret>0){
            return R.ok();
        }else {
            return R.error();
        }
    }

}

